package com.example.zhujiemian;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zhujiemian.Bean.Course;

import com.example.zhujiemian.db.DBHelper;

public class AddCourse extends AppCompatActivity implements View.OnClickListener {
    private TextView mTextView;
    private EditText mEditCourseId;
    private EditText mEditCourserName;
    private EditText mEditCourserTeacher;
    private EditText mEditStudyTime;
    private DBHelper DBHelper;
    private Button mbuttonAddCourseOk;
    private Button mbuttonCoursebackSet;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_course);
        DBHelper =new DBHelper(this);
        mbuttonAddCourseOk=findViewById(R.id.buttonAddCourseOk);
        mbuttonAddCourseOk.setOnClickListener(this);
        mbuttonCoursebackSet=findViewById(R.id.buttonCoursebackSet);
        mbuttonCoursebackSet.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String id = mEditCourseId.getText().toString();
        String name = mEditCourserName.getText().toString();
        String teacher = mEditCourserTeacher.getText().toString();
        String studytime = mEditStudyTime.getText().toString();

        if (v.getId() == R.id.buttonAddCourseOk) {
            if (allInputNotEmpty(id,name,teacher,studytime)) {
                Course course=new Course();
                course.setCourse_id(id);
                course.setCourse_name(name);
                course.setCourse_teacher(teacher);
                course.setStudytime(studytime);
                int id1 = (int) DBHelper.getInstance(this).insertCourse2(course);
                if (id1 != -1) {
                    Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                    mEditCourseId.setText("");
                    mEditCourserName.setText("");
                    mEditCourserTeacher.setText("");
                    mEditStudyTime.setText("");
                }else {
                    Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
                }
            }
        }else if(v.getId() == R.id.buttonCoursebackSet){
            mbuttonCoursebackSet.setVisibility(View.VISIBLE);
            Intent intent=new Intent(AddCourse.this,Setting.class);
            startActivity(intent);
        }
    }
    private boolean allInputNotEmpty(String... inputStrings) {
        for (int i = 0; i < inputStrings.length; i++) {
            if (TextUtils.isEmpty(inputStrings[i])) {
                return false;
            }
        }
        return true;
    }
}

